<?php


namespace app\api\controller\v1;
use app\common\controller\BaseController;
use think\facade\Log;

class VersionController extends BaseController
{
    function checkAction()
    {
        $this->paramlist=[
            'appid' => 'string',
            'version' => 'string',
            'platform' => 'string',
        ];

        $this->check_params();

        $version = config('app.version');

        $update = 0;
        if ($this->argv['appid'] != $version['id']){
            $this->throw_myexception(0, "not this app");
        }


        $currentVersions = explode('.',$this->argv['version']);

        $configv = null;
        $configurl = null;

        if ($this->argv['platform'] =='android') {
            $configv = $version['androidv'];
            $configurl = $version['androidurl'];
        }
        else {
            $configv = $version['iosv'];
            $configurl = $version['iosurl'];
        }


        $resultVersions  = explode('.',$configv);

        Log::info('$currentVersions:{v}',['v' =>$this->argv['version']]);
        Log::info('$resultVersions:{v}',['v' => $configv]);

        if (intval($currentVersions[0]) < intval($resultVersions[0]))
        {
            //大版本更新
            $update = 1;
            $this->resp['note'] = $version['note'];
            $this->resp['url'] = $configurl;
        }
        else
        {
            //小版本
            if (intval($currentVersions[1]) < intval($resultVersions[1]))
            {
                $update = 1;
                $this->resp['note'] = $version['note'];
                $this->resp['wgtUrl'] = $version['wgtUrl'];
            }
            else if (intval($currentVersions[1]) == intval($resultVersions[1]))
            {
                if (intval($currentVersions[2]) < intval($resultVersions[2])){
                    $update = 1;
                    $this->resp['note'] = $version['note'];
                    $this->resp['wgtUrl'] = $version['wgtUrl'];
                }
            }
        }

        $this->resp['update'] = $update;

        return $this->ret_resp();
    }

    function versionAction()
    {
        $version = config('app.version');
        $this->resp['data'] = $version;

        return $this->ret_resp();
    }
}